//
//  NSString+QCURLSchemeUtil.m
//  KDFDApp
//
//  Created by Bruce on 2017/10/23.
//  Copyright © 2017年 cailiang. All rights reserved.
//

#import "NSString+QCURLSchemeUtil.h"
#import "FQJumpAction.h"
#import "FQSimpleWKWebVC.h"
static NSString *schemeKey = @"qunajie";
@implementation NSString (QCURLSchemeUtil)

#pragma mark -
- (BOOL)checkOpenApplicationURL
{
    NSString *scheme = [NSURL URLWithString:self].scheme;
    if (![scheme isEqual:@"http"]&&![scheme isEqual:@"https"]&&![scheme isEqual:schemeKey]) {
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self] options:@{} completionHandler:^(BOOL success) {
                       
                   }];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self]];
        }
        return true;
    }else{
        if ([self hasPrefix:@"https://itunes.apple.com"]||[self hasPrefix:@"http://itunes.apple.com"]) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:self]];
            return true;
        }
        return false;
    }
    return false;
}

- (void)resolveURLWithSource:(UIViewController *)vc resultBlock:(void (^)(NSString *url))resolveSuccess{
    //首先对url中的特殊字符做一下处理（例如中文、#+？等等）防止拿不到url的scheme
    NSString *url = self;
    if (url) {
        NSURL *URL = [NSURL URLWithString:url];
        NSString *scheme = [URL scheme];
        if ([scheme isEqualToString:schemeKey]) {
            NSMutableDictionary *dic = [FQJumpAction rsolve:url];
            FQJumpAction *action = [FQJumpAction new];
            [action jumpNew:dic entity:nil isPush:false];
            return;
        }else if ([scheme isEqualToString:@"http"]||[scheme isEqualToString:@"https"]){
            if (resolveSuccess) {
                resolveSuccess(self);
            }else{
                FQSimpleWKWebVC *web = [[FQSimpleWKWebVC alloc]init];;
                [web loadWebURLSring:self];
                if (vc.navigationController) {
                    [vc.navigationController pushViewController:web animated:YES];
                }else{
                   
                }
            }
            return;
        }else{
            if (self && ![self isEqual:@""] && !URL) {
            }
            [[UIApplication sharedApplication] openURL:URL];
            return;
        }
    }
}
@end
